package com.gitee.srurl.system.controller.open;

import cn.hutool.core.util.StrUtil;
import com.gitee.srurl.system.biz.ShortUrlRedirectBiz;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @author zxw
 * @date 2023/4/29 12:02
 */
@RestController
@RequiredArgsConstructor
public class ShortUrlRedirectController {

    private final ShortUrlRedirectBiz shortUrlRedirectBiz;

    /**
     * 重定向到真实网站
     *
     * @param shortId 短链ID
     * @return 重定向地址
     */
    @GetMapping("{shortId}")
    public String redirectRealWebSite(@PathVariable String shortId, HttpServletResponse response) throws IOException {

        String originUrl = shortUrlRedirectBiz.getShortUrlRedirectIfEffectivity(shortId);
        if (StrUtil.isBlank(originUrl)) {
            return "地址不存在或已失效";
        }
        response.sendRedirect(originUrl);
        return "";
    }
}
